package com.mison.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mison.model.MisonFeedbackPerson;
import com.mison.utils.DBUtils;
import com.mison.utils.Page;

public class LiuswDao {

	public List<MisonFeedbackPerson> getList(Page<MisonFeedbackPerson> page) {
		Connection conn = DBUtils.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		List<MisonFeedbackPerson> list = new ArrayList<MisonFeedbackPerson>();
		
		String sql = "select * from mison_feedback_person limit ?,?";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page.getCurrPage()-1)*page.getPerPage());
			ps.setInt(2, page.getPerPage());
			
			System.out.println(ps);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				MisonFeedbackPerson m = new MisonFeedbackPerson();
				m.setFeedbackPersonId(rs.getInt("feedback_person_id"));
				m.setName(rs.getString("name"));
				m.setType(rs.getInt("type"));
				m.setPicPath(rs.getString("pic_path"));
				m.setSummary(rs.getString("summary"));
				m.setIsLeave(rs.getInt("is_leave"));
			    list.add(m);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return list;
	}

	public int getCount() {
		Connection conn = DBUtils.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "select count(*) from mison_feedback_person";
		
		try {
			ps = conn.prepareStatement(sql);	
			
			System.out.println(ps);
			
			rs = ps.executeQuery();
			
			rs.next();
			
			return rs.getInt("count(*)");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return 0;
	}

}
